﻿using LocoyApp.Locoy.Entry;

namespace LocoyApp.Locoy
{
    public class ImagePicker : LocoyCode
    {
        /// <summary>
        /// 执行方法，不能修改类和方法名称。
        /// </summary>
        /// <param name="content">标签内容</param>
        /// <param name="response">页面响应，包含了Url、原始Html等属性</param>
        /// <returns>返回处理后的标签内容</returns>
        public string Run(string content, ResponseEntry response)
        {
            if (string.IsNullOrEmpty(content) || string.IsNullOrWhiteSpace(content) || content.IndexOf("src=") == -1)
            {
                return "";
            }

            var regex = new System.Text.RegularExpressions.Regex(@"src=""(.+?)""");
            var matched = regex.Match(content);
            if (matched.Success)
            {
                var img = matched.Groups[1].Value;
                if (img.StartsWith("http") || img.StartsWith("/"))
                    return img;
                else
                    return "/" + img;
            }

            return "";
        }
    }
}
